<?php /** @noinspection DuplicatedCode */
/**
 * 每个任务下每天获取经验值的人数
 * 每天中奖uid、昵称、粉丝数
 * 每天抽奖人数
 */


ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
error_reporting(E_ERROR | E_PARSE);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityModel;
use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityWinnerListModel;
use Doraemon\cache\UserCounter;
use Biz\pockets\excel\DownExcel;
use Biz\tools\Mail;
use Doraemon\service\User;
use Doraemon\tools\Log;


const EMAIL_MAP = [
    'renzhifan@haohaozhu.com',
    'chenhuixian@haohaozhu.com',
    'zhangfenglin@haohaozhu.com',
    'wangjianwei@haohaozhu.com',
];

try {
    $startTime = $argv[1] ?? date("Y-m-d", strtotime("-1 day", time()));
    $endTime = $argv[2] ?? date('Y-m-d H:i:s', strtotime($startTime) + 86399);
    $lastDay = date("Ymd",strtotime("-1 day"));
    sendGzDesignWeekJoinList($startTime, $endTime,$lastDay);
    sendGzDesignWeekWinnerList($lastDay);
    sendToDayWinnerCount($lastDay);


} catch (Exception $exception) {
    Log::error('ExportIkeaActivity.php', $exception);
}

function sendGzDesignWeekJoinList($startTime, $endTime,$lastDay)
{
    $where = [
        'addtime[>=]' => $startTime,
        'addtime[<=]' => $endTime,
        'status' => GzDesignWeekActivityModel::STATUS_NORMAL,
        'GROUP' => [
            'task_id'
        ]
    ];
    $joinList = GzDesignWeekActivityModel::getCountMaps($where);
    if (!empty($joinList)) {
        $joinListMap = array_column($joinList, 'cnt','task_id');
        $taskMap = GzDesignWeekActivityModel::TASK_MAP;
        $list = [];
        $toDay =date("Y-m-d");
        foreach ($taskMap as $task){
            $list[]=[
                'day'=>$toDay,
                'task_id'=>$task['task_id'],
                'count'=>$joinListMap[$task['task_id']]??0,
            ];
        }
        $pageTitle = '广州设计周-'.$lastDay.'-每个任务领取经验值人数统计';

        $filePath = exportGzDesignWeekJoinList($list,$pageTitle);
        Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$startTime | $endTime", $filePath);
        echo '发送邮件成功'.PHP_EOL;
    }

}

function sendGzDesignWeekWinnerList($lastDay)
{
    $where = [
        'day' => $lastDay,
        'type' => GzDesignWeekActivityWinnerListModel::TYPE_1,
        'status' => GzDesignWeekActivityWinnerListModel::STATUS_NORMAL,
    ];
    $winnerList = GzDesignWeekActivityWinnerListModel::selectByWhere($where);
    if (!empty($winnerList)) {
        $winnerListUidArr = array_column($winnerList, 'uid');

        $counterMap = UserCounter::getMultiByUids($winnerListUidArr);
        $userList = User::getBatchBasicUserInfo($winnerListUidArr);
        $userList = array_column($userList, null, 'uid');
        $list = [];
        foreach ($winnerList as $winner) {
            $list[] = [
                'day' => $lastDay,
                'uid' => $winner['uid'],
                'nick' => $userList[$winner['uid']]['nick'] ?? '',
                'fans' => $counterMap[$winner['uid']][UserCounter::TYPE_FANS] ?? 0,
            ];
        }
        $pageTitle = '广州设计周-'.$lastDay.'中奖uid、昵称、粉丝数';
        $filePath = exportGzDesignWeekWinnerList($list,$pageTitle);
        Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$lastDay", $filePath);
        echo '发送邮件成功'.PHP_EOL;
    }

}

function sendToDayWinnerCount($lastDay)
{
    $drawCount = GzDesignWeekActivityWinnerListModel::getLastDayDrawCountByDay($lastDay);
    $list[]=[
        'day'=>$lastDay,
        'count'=>$drawCount,
    ];
    $header = [
        '日期',
        '抽奖人数',
    ];
    $pageTitle = '广州设计周-'.$lastDay.'-抽奖人数';
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName($pageTitle);
    $excel_obj->setSheetTitle($pageTitle);

    // 设置列宽
    $widths = [
        'A' => 30,
        'B' => 30,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();
    $filePath = ($excel_obj->create($list, $header));
    Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$lastDay", $filePath);
    echo '发送邮件成功'.PHP_EOL;
    
}

//导出完成任务名单
function exportGzDesignWeekJoinList($list,$pageTitle)
{
    $header = [
        '日期',
        '任务名称',
        '领取经验值人数',
    ];
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName($pageTitle);
    $excel_obj->setSheetTitle($pageTitle);

    // 设置列宽
    $widths = [
        'A' => 30,
        'B' => 30,
        'C' => 30,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();

    return ($excel_obj->create($list, $header));
}

//导出中奖名单
function exportGzDesignWeekWinnerList($list,$pageTitle)
{
    $header = [
        '日期',
        'uid',
        '昵称',
        '粉丝数',
    ];
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName($pageTitle);
    $excel_obj->setSheetTitle($pageTitle);

    // 设置列宽
    $widths = [
        'A' => 30,
        'B' => 30,
        'C' => 30,
        'D' => 30,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();

    return ($excel_obj->create($list, $header));
}
